<template>
  <div class="reportlist">
    <van-nav-bar title="移动报表" left-text="返回" left-arrow @click-left="backToMain" />
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="已加载全部数据" @load="onLoad">
        <van-cell v-for="item in list" clickable :key="item.title" :title="item.title" @click="openReport(item)" />
      </van-list>
    </van-pull-refresh>
    <van-tabbar v-model="buttomactive" safe-area-inset-bottom>
      <van-tabbar-item icon="home-o" @click="menuChange('homepage')">主页</van-tabbar-item>
      <van-tabbar-item icon="label-o" @click="menuChange('flowlist')">审批</van-tabbar-item>
      <van-tabbar-item icon="qr" @click="menuChange('main')">功能</van-tabbar-item>
      <van-tabbar-item icon="chart-trending-o" @click="menuChange('report')">分析</van-tabbar-item>
      <van-tabbar-item icon="user-o" @click="menuChange('mine')">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, Dialog, Checkbox, CheckboxGroup, Panel, NavBar } from 'vant'
import qs from 'qs'
Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(Dialog).use(Checkbox).use(CheckboxGroup).use(Panel).use(NavBar)
export default {
  name: 'reportlist',
  data () {
    return {
      idobject: this.$route.query.idobject,
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      buttomactive: 3
    }
  },
  methods: {
    onLoad () {
      let data = {}
      this.axios({
        method: 'post',
        url: '/api/' + this.idobject + '.list.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          let rsdata = res.data.jsondata.reportlist
          for (let group in rsdata) {
            for (let i = 0; i < rsdata[group].length; i++) {
              this.list.push(rsdata[group][i])
            }
          }
          if (rsdata == null || rsdata.length === 0 || rsdata.length < this.pagecount) {
            this.loading = false
            this.finished = true
          } else {
            this.finished = true
            this.loading = false
          }
        }
      }).catch(res => {
        this.reserror(res)
      })
    },
    onRefresh () {
      this.list = []
      this.startline = 1
      this.finished = false
      this.loading = true
      this.isLoading = false
      this.onLoad()
    },
    menuChange (option) {
      if (option === 'flowlist') {
        this.$router.push({ name: 'flowlist', query: { idobject: 'flowclmx', actiontype: 'mytask', actioncode: 'unhandle', stitle: 'flowclmx' } })
      } else if (option === 'report') {
        this.$router.push({ name: 'reportlist', query: { idobject: 'report', stitle: 'report' } })
      } else if (option === 'main') {
        this.$router.push({ name: 'main' })
      } else if (option === 'homepage') {
        this.$router.push({ name: 'homepage' })
      } else if (option === 'mine') {
        this.$router.push({ name: 'mine' })
      }
    },
    openReport (item) {
      this.$router.push({ name: 'reportresult', query: { reporttitle: item.title, reportid: item.idreport, reportviewid: item.idview } })
    },
    backToMain () {
      this.$router.push('/main')
    }
  }
}
</script>
